package com.rran.study.designpattern.behavioral.chain;

import com.rran.study.designpattern.behavioral.chain.chain.spring.ChainSpringHandler;
import com.rran.study.designpattern.behavioral.chain.enums.ChainType;
import com.rran.study.designpattern.behavioral.chain.factory.ChainHandlerFactory;
import com.rran.study.designpattern.behavioral.chain.model.ChainContext;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ConfigurableApplicationContext;


/**
 * 模拟攻击ABCDE点,从A点开始->E点
 * 责任链+工厂+模板+springboot实现
 * 通过自定义注解的方式定义责任链，在spring容器加载时初始化到缓存中,比起static代码更优雅
 * 结合模板模式，子类可以在责任链执行过程中（每一步成功，失败）自定义方法
 * 可以在定义的责任链中间位置开始执行
 */
@EnableAutoConfiguration
public class ChainSpringApplication {

    public static void main(String[] args) {

        //非web方式启动
        ConfigurableApplicationContext context = new SpringApplicationBuilder(ChainSpringApplication.class)
                .web(WebApplicationType.NONE)
                .run(args);

        //从A点开始进攻 A->B->C->D->E
        ChainContext chainContext = new ChainContext();
        chainContext.setChainType(ChainType.ATTACK);
        chainContext.setAction("ATTACK_A");


        ChainSpringHandler headHandler = ChainHandlerFactory.getChainHeadHandler(chainContext);
        headHandler.handle(chainContext);

        System.out.println("======================================");

        //从B点开始进攻 B->C->D->E
        ChainContext chainContext2 = new ChainContext();
        chainContext2.setChainType(ChainType.ATTACK);
        chainContext2.setAction("ATTACK_B");


        ChainSpringHandler headHandler2 = ChainHandlerFactory.getChainSpringHandler(chainContext2);
        headHandler2.handle(chainContext2);

        //关闭上下文
        context.close();

    }
}
